package com.homedo.microservice.open.service.gateway.persistence.dao;

import com.homedo.common.dao.BaseDao;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.bean.enums.forzuulfilter.ProgressEnum4ZuulFilter;
import com.homedo.microservice.open.service.gateway.bean.enums.forzuulfilter.StatusEnum4ZuulFilter;
import com.homedo.microservice.open.service.gateway.bean.po.ZuulFilter;
import com.homedo.microservice.open.service.gateway.persistence.mapper.IZuulFilterMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author 作者 jgshun
 * @date 创建时间 2018年09月10日 16时28分40秒
 */
@Repository
public class ZuulFilterDao extends BaseDao<ZuulFilter> {
    @Autowired
    private IZuulFilterMapper zuulFilterMapper;

    public List<ZuulFilter> getList4Application(String applicationName, List<ProgressEnum4ZuulFilter> progresses, List<StatusEnum4ZuulFilter> statuses) {
        Example example = new Example(ZuulFilter.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo(ZuulFilter.FIELD_APPLICATIONNAME, applicationName);

        if (Detect.notEmpty(progresses)) {
            criteria.andIn(ZuulFilter.FIELD_PROGRESS, progresses.stream().map(ProgressEnum4ZuulFilter::getCode).collect(Collectors.toList()));
        }

        if (Detect.notEmpty(statuses)) {
            criteria.andIn(ZuulFilter.FIELD_STATUS, statuses.stream().map(StatusEnum4ZuulFilter::getCode).collect(Collectors.toList()));
        }

        return super.getListByExample(example);
    }


}
